/****************************************************
	文件：CustomRenderPipeline.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/9/9 11:37:58
	功能：自定义渲染管线  实例（由 CustomRenderPipelineAsset 创建并返回给 Unity）
*****************************************************/

using System;
using UnityEngine;
using UnityEngine.Rendering;

namespace LearningSRP
{
    public class CustomRenderPipeline : RenderPipeline
    {
        /// <summary>
        /// 执行单个相机的渲染
        /// </summary>
        private CameraRenderer mRenderer = new CameraRenderer();

        private bool mUseDynamicBatching;
        private bool mUseGPUInstancing;
        /// <summary>
        /// 阴影设置
        /// </summary>
        private ShadowSettings mShadowSettings;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="useSRPBatcher">是否使用SRPBatcher</param>
        /// <param name="useDynamicBatching">是否使用动态批处理</param>
        /// <param name="useGPUInstancing">是否使用GPU实例</param>
        public CustomRenderPipeline(bool useSRPBatcher, bool useDynamicBatching, bool useGPUInstancing, ShadowSettings shadowSettings)
        {
            //SRP Batching
            GraphicsSettings.useScriptableRenderPipelineBatching = useSRPBatcher;
            //动态批处理
            mUseDynamicBatching = useDynamicBatching;
            //GPU实例
            mUseGPUInstancing = useGPUInstancing;
            mShadowSettings = shadowSettings;
            GraphicsSettings.lightsUseLinearIntensity = true; //Light color 使用线性空间
        }

        /// <summary>
        /// Unity每帧调用此函数，进行渲染
        /// </summary>
        /// <param name="context">渲染上下文</param>
        /// <param name="cameras">相机组</param>
        protected override void Render(ScriptableRenderContext context, Camera[] cameras)
        {
            foreach (Camera camera in cameras)
            {
                mRenderer.Render(context, camera, mUseDynamicBatching, mUseGPUInstancing);
            }
        }
    }
}
